//
//  SDHSportsTeamLineupVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/9.
//

#import "SDHSportsTeamLineupVC.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "SDHSportsTeamDataModel.h"
#import "MJExtension.h"
#import "SDHSportsLineupCell.h"

@interface SDHSportsTeamLineupVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTable;
@property (nonatomic, strong) SDHSportsTeamDataModel *dataModel;

@end

@implementation SDHSportsTeamLineupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initUI];
    [self loadData];
}

- (void)initUI {
    UITableView *table = [UITableView new];
    table.backgroundColor = [[UIColor whiteColor] addAlpha:0.7];
    table.dataSource = self;
    table.delegate = self;
    table.estimatedRowHeight = 110;
    table.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:table];
    self.dataTable = table;
    
    __weak typeof(self) weakSelf = self;
    [self.dataTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsZero);
        make.bottom.equalTo(weakSelf.view.mas_top).offset(kScreenHeight-255);
    }];

}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    
    [self.dataTable reloadData];
}

- (void)parseData:(NSDictionary *)dic {
    _dataModel = [SDHSportsTeamDataModel mj_objectWithKeyValues:dic[@"data"]];
}

- (void)loadData {
    NSString *teamId = self.tabInfoDic[@"teamInfo"][@"team_id"];
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyu1/teamLineup";
    if (teamId.length == 0) {
        teamId = @"145868";
    }
    request.requestPara = @{@"id":teamId};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataModel.players.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsLineupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsLineupCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    SDHSportsTeamDataPlayerModel *model = [_dataModel.players objectAtIndex:indexPath.row];
    [cell updateContent:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsTeamDataPlayerModel *model = [_dataModel.players objectAtIndex:indexPath.row];
}


@end
